//
//  LiuNetworkManager.swift
//  LiuWeiBo
//
//  Created by 刘晓晨 on 2017/3/28.
//  Copyright © 2017年 刘晓晨. All rights reserved.
//

import UIKit
import AFNetworking
enum requestType {
    case get
    case post
}

//网络加载工具
class LiuNetworkManager: AFHTTPSessionManager {
    
    //全局访问点
    static let sharedTools:LiuNetworkManager = {
        let manager = LiuNetworkManager()
        manager.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return manager
        
    }()
    
    fileprivate func request(type:requestType, urlStr:String, pramas:Any,callback:@escaping (Any?,Error?)->()) {
        
        //判断请求方式
        if type == .get {
            get(urlStr, parameters: pramas, progress: nil, success: { (_, response) in
                
                callback(response,nil)
                
            }, failure: { (nil, error) in
                
                callback(nil,error)
            })
        } else {
            
            //post请求
            post(urlStr, parameters: pramas, progress: nil, success: { (_, response) in
                
                callback(response,nil)
            }, failure: { (_, error) in
                
                callback(nil,error)
            })
        }
        
    }
}

extension LiuNetworkManager {
    
    //获取token网络接口
    func requestToken(code:String, compeleteCallBack:@escaping (Any?,Error?)->()) {
        
        //urlStr
        let urlStr = "https://api.weibo.com/oauth2/access_token"
        
        //请求参数
        let parameters:[String:Any] = [
            
            "client_id":client_id,
            "client_secret":client_secret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":redirect_Url
        ]
        
        request(type: .post, urlStr: urlStr, pramas: parameters, callback: compeleteCallBack)
        
        
    }
    
    //根据token请求微博用户数据
    func requestUserInfor(userAccount:LiuUserAccount, compeleteCallBack:@escaping (Any?,Error?)->()) {
        
        //urlStr
        let urlStr = "https://api.weibo.com/2/users/show.json"
        
        //请求参数
        let parameters:[String:Any] = [
            
            "access_token":userAccount.access_token!,
            "uid":userAccount.uid,
        ]
        
        request(type: .get, urlStr: urlStr, pramas: parameters, callback: compeleteCallBack)
    }
    
    //根据token获取关注人的微博数据
    func requestHomeData(sinceId:Int64 = 0, maxId:Int64 = 0, userAccount:LiuUserAccount, compeleteCallBack:@escaping (Any?,Error?)->()) {
        let urlStr = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        //请求参数
        let parameters:[String:Any] = [
            
            "access_token":userAccount.access_token!,
            "since_id":sinceId,
            "max_id":maxId
            ]
        
        request(type: .get, urlStr: urlStr, pramas: parameters, callback: compeleteCallBack)
        
        
    }
    
    ///发微博(只有文字)
    func sendWeiboOnlyText(userAccount:LiuUserAccount, text:String, compeleteCallBack:@escaping (Any?,Error?)->()) {
        let url = "https://api.weibo.com/2/statuses/update.json"
        
        //请求参数
        let parameters:[String:Any] = [
            
            "access_token":userAccount.access_token!,
            "status":text
        ]
        
        request(type: .post, urlStr: url, pramas: parameters, callback: compeleteCallBack)
    }
    
    ///发微博(带一张图片)
    func sendWeiboWithImage(userAccount:LiuUserAccount, text:String, image:UIImage, compeleteCallBack:@escaping (Any?,Error?)->()) {
        let url = "https://upload.api.weibo.com/2/statuses/upload.json"
        
        var textStr = text
        
        if text == "" {
            textStr = "分享图片"
        }
        //请求参数
        let parameters:[String:Any] = [
            
            "access_token":userAccount.access_token!,
            "status":textStr
        ]
        
        //获取图片的二进制文件
        let imageData = UIImageJPEGRepresentation(image, 0.5)!
        
        post(url, parameters: parameters, constructingBodyWith: { (data) in
            data.appendPart(withFileData: imageData, name: "pic", fileName: "sendPic", mimeType: "image/jpeg")
        }, progress: nil, success: { (_, response) in
            compeleteCallBack(response,nil)
        }) { (_, error) in
            compeleteCallBack(nil, error)
        }
        
    }
}




